#define _CRT_SECURE_NO_WARNINGS 1
#include"stack.h"

void StackInit(ST* p)
{
	assert(p);
	p->arr = (StackDataType*)malloc(4*sizeof(StackDataType));
	p->capacity = 4;
	p->top = -1;
}

void StackDestory(ST* p )
{
	assert(p);
	free(p->arr);
	p->arr = NULL;
	p->capacity = 0;
	p->top = -1;
}

void StackPush(ST* p, StackDataType x)
{
	assert(p);
	p->top++;
	if (p->capacity == p->top)
	{
		StackDataType* temp = (StackDataType*)realloc(p->arr, 2 * p->capacity * sizeof(StackDataType));
		if (temp == NULL)
		{
			printf("malloc failed!\n");
		}
		else
		{
			p->capacity *= 2;
			p->arr = temp;
		}
	}
	p->arr[p->top] = x;
}

void StackPop(ST* p)
{
	assert(p);
	assert(p->top > -1);
	p->top--;
}

StackDataType StackTop(ST* p)
{
	assert(p);
	assert(p->top >= 0);
	return p->arr[p->top];
}

int StackSize(ST* p)
{
	assert(p);
	return ++p->top;
}

bool StackEmpty(ST* p)
{
	if (p->top == -1)
		return true;
	else
		return false;
}